package com.itheima.reggie_auto.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie_auto.common.R;
import com.itheima.reggie_auto.dto.SetmealDto;
import com.itheima.reggie_auto.entity.Setmeal;
import com.itheima.reggie_auto.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author 刘少宽
 * @since 2022-09-23
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//删除所有套餐缓存，因为这里没有传分类id，没法根据key来进行删
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.save(setmealDto);
        return R.success("保存套餐成功");
    }

    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name) {

        Page<SetmealDto> pageInfo = setmealService.page(page,pageSize,name);
        return R.success(pageInfo);
    }

    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//删除所有套餐缓存，因为这里没有传分类id，没法根据key来进行删
    public R<String> deleteByIds(@RequestParam("ids") List<Long> ids) {
        setmealService.deleteByIds(ids);
        return R.success("删除套餐成功");
    }

    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)//删除所有套餐缓存，因为这里没有传分类id，没法根据key来进行删
    public R<String> updateStatus(@PathVariable Integer status, @RequestParam("ids") Long[] ids) {

        setmealService.updateStatus(status,ids);

        return R.success("修改状态成功");
    }


    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id) {

        SetmealDto setmealDto = setmealService.getById(id);
        return R.success(setmealDto);
    }

    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//删除所有套餐缓存，因为这里没有传分类id，没法根据key来进行删
    public R<String> updateById(@RequestBody SetmealDto setmealDto) {
        setmealService.updateById(setmealDto);
        return R.success("修改套餐成功");
    }

    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        List<Setmeal> list =  setmealService.getList(setmeal);
        return R.success(list);
    }

}

